package com.share.device.controller;

import com.alibaba.fastjson2.JSONObject;
import com.share.common.core.domain.R;
import com.share.common.core.web.domain.AjaxResult;
import com.share.device.service.IMapService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static com.share.common.core.web.domain.AjaxResult.success;

@Slf4j
@Tag(name = "地图接口管理")
@RestController
@RequestMapping("/map")
public class MapController {
    @Autowired
    private IMapService mapService;

    @Operation(summary = "根据经纬度计算详细地址")
    @GetMapping("/calculateLatLng/{keyword}")
    public AjaxResult calculateLatLng(@PathVariable String keyword)
    {
        return success(mapService.calculateLatLng(keyword));
    }

    @Operation(summary = "获取当前位置")
    @GetMapping("/location")
    public R<Map<String, Object>> getCurrentLocation() {
        try {
            log.info("开始获取当前位置");
            JSONObject location = mapService.getCurrentLocation();
            
            Map<String, Object> data = new HashMap<>();
            data.put("lat", location.getDouble("lat"));
            data.put("lng", location.getDouble("lng"));
            
            log.info("获取当前位置成功：{}", data);
            return R.ok(data);
        } catch (Exception e) {
            log.error("获取当前位置失败：{}", e.getMessage());
            return R.fail("获取位置失败：" + e.getMessage());
        }
    }
}
